package com.ddzh.web;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ddzh.entity.Result;
import com.ddzh.pojo.ExportDataVo;
import com.ddzh.service.ExportDataService;
import com.ddzh.utils.DateUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/exportData")
public class ExportDataController {


    @Reference
    private ExportDataService exportDataService;

    @GetMapping("/getAllData")
    public Result getAllData(){

        return  new Result(exportDataService.getAllData());
    }


    @PostMapping("/export")
    public void  Export(@RequestBody ExportDataVo exportDataVo)throws Exception{
        System.out.println("***********"+exportDataVo);
        TemplateExportParams  params = new TemplateExportParams(
                "report_template.xlsx");
        Map data=new HashMap<>();

        String date = DateUtil.formatDate(new Date());
        System.out.println(date);
        data.put("reportDate",date);
        data.put("todayNewMember",exportDataVo.getTodayNewMember());
        data.put("totalMember",exportDataVo.getTotalMember());
        data.put("thisWeekNewMember",exportDataVo.getThisWeekNewMember());
        data.put("thisMonthNewMember",exportDataVo.getThisMonthNewMember());
        data.put("todayOrderNumber",exportDataVo.getTodayOrderNumber());
        data.put("todayVisitsNumber",exportDataVo.getTodayVisitsNumber());
        data.put("thisWeekOrderNumber",exportDataVo.getThisWeekOrderNumber());
        data.put("thisWeekVisitsNumber",exportDataVo.getThisWeekVisitsNumber());
        data.put("thisMonthOrderNumber",exportDataVo.getThisMonthOrderNumber());
        data.put("thisMonthVisitsNumber",exportDataVo.getThisMonthVisitsNumber());
        //data.put("hotSetmeal",exportDataVo.getHotSetmeal());
        //System.out.println((String) exportDataVo.getHotSetmeal().get(1).get("name"));


//        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
//        for (int i = 0; i < 1; i++) {
//            Map<String, String> lm = new HashMap<String, String>();
////            lm.put("name", (String) exportDataVo.getHotSetmeal().get(i).get("name"));
//            lm.put("counts",exportDataVo.getHotSetmeal().get(i).get("count")+"");
//          /*  lm.put("counts", "20");*/
//            lm.put("zhanbi", "12.5%");
//            lm.put("remark", "无");
//
//            listMap.add(lm);
//        }
//        data.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, data);
        OutputStream os= new FileOutputStream("D:/text.xlsx");
        workbook.write(os);
        os.flush();
        os.close();

    }












}
